package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {
    /**
     * 分页查询
     *
     * @param extCproductExample
     * @param pageNum
     * @param pageSize
     */

    @Autowired
    private ExtCproductDao extCproductDao;

@Autowired
private ContractDao contractDao;



    @Override
    public PageInfo<ExtCproduct> findByPage(ExtCproductExample extCproductExample, int pageNum, int pageSize) {
        //1.设置查询开始第一页查询
        PageHelper.startPage(pageNum ,pageSize);
        //2.调用dao,查询所有
        List<ExtCproduct> list = extCproductDao.selectByExample(extCproductExample);

        //3.返回根据分页得到的所有数据
        return new PageInfo<>(list);
    }

    /**
     * 查询所有
     *
     * @param extCproductExample
     */
    @Override
    public List<ExtCproduct> findAll(ExtCproductExample extCproductExample) {
        return extCproductDao.selectByExample(extCproductExample);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param extCproduct
     */
    @Override
    public void save(ExtCproduct extCproduct) {
        //1.设置id
        extCproduct.setId(UUID.randomUUID().toString());

        //2.计算附件金额
        Double amout=0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null) {
            amout = extCproduct.getCnumber() * extCproduct.getPrice();
        }
       extCproduct.setAmount(amout);
        //3.修改购销合同
        //根据购销合同id去查询得到购销合同
        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);

        //总金额
        contract.setTotalAmount(contract.getTotalAmount()+amout);

        //附件数量
        contract.setExtNum(contract.getImportNum()+1);
        //修改购销合同
        contractDao.updateByPrimaryKeySelective(contract);
        //添加附件
        extCproductDao.insertSelective(extCproduct);
    }

    /**
     * 修改
     *
     * @param extCproduct
     */
    @Override
    public void update(ExtCproduct extCproduct) {
        //1.计算附件金额
        Double amount=0d;
        if (extCproduct.getPrice()!=null && extCproduct.getCnumber()>0){
            amount=extCproduct.getCnumber()*extCproduct.getPrice();
            //保存进去
            extCproduct.setAmount(amount);
        }
        //2.根据附加id查询附件，获取修改前的附近金额
        ExtCproduct cproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        //2.1修改前附件金额
        Double oldAmount = cproduct.getAmount();

        //3.修改购销合同总金额
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //修改总金额
        contract.setTotalAmount(contract.getTotalAmount()+amount-oldAmount);
        //修改购销合同
        contractDao.updateByPrimaryKeySelective(contract);
        //修改附件
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }




    /**
     * 删除部门
     *
     * @param id
     */
    @Override
    public void delete(String id) {

        //1. 根据附件id查询获取附件金额
        ExtCproduct extc =
                extCproductDao.selectByPrimaryKey(id);

        //获取附件金额
        Double amount = extc.getAmount();

        //2. 修改购销合同总金额
        Contract contract =
                contractDao.selectByPrimaryKey(extc.getContractId());
        //A. 总金额 = 总金额 - 附件金额
        contract.setTotalAmount(contract.getTotalAmount() - amount);
        //B. 修改附件数量减1
        contract.setExtNum(contract.getExtNum() - 1);
        //C. 修改购销合同
        contractDao.updateByPrimaryKeySelective(contract);

        //3. 删除附件
        extCproductDao.deleteByPrimaryKey(id);


    }
}
